//
//	File: OyFontManager.cpp
//

#include "OyFontManager.h"
#include "..\..\OyGeneral\OyGeneral.h"

OyFontManager::OyFontManager() : m_OyFontList(INIT_FONTLIST_ARRAY_NUM)
{
	m_pSkinManager = NULL;
	m_pDevice = NULL;
	m_pOyD3D9 = NULL;
	m_ActiveFont = NULL;
}

OyFontManager::~OyFontManager()
{
	Release();
}

bool OyFontManager::Initialize( OySkinManager *pSkinManager, LPDIRECT3DDEVICE9 pDevice, OyDirect3D9 *pOyD3D9 )
{

	m_pSkinManager = pSkinManager;
	m_pDevice = pDevice;
	m_pOyD3D9 = pOyD3D9;


	if( FT_Init_FreeType(&m_FTLib) != 0 )
	{
		OyLog_Write_L1(OyXMLLog::Error, OyXMLLog::Engine, L"error: Initialize Free Type Fail!");
		return false;
	}

	return true;
}

void OyFontManager::Release(void)
{
	for( int i=0; i<m_OyFontList.Count(); i++ )
	{
		Oy_TTF_Font* pFont = m_OyFontList[i];
		pFont->UnloadFont();
		delete pFont;
		pFont = NULL;
	}

	FT_Done_FreeType(m_FTLib);
}

void OyFontManager::OnDeviceLost(void)
{
	for( int i=0; i<m_OyFontList.Count(); i++ )
	{
		Oy_TTF_Font* pFont = m_OyFontList[i];
		pFont->OnDeviceLost();
	}
}

void OyFontManager::OnDeviceReset(void)
{
	for( int i=0; i<m_OyFontList.Count(); i++ )
	{
		Oy_TTF_Font* pFont = m_OyFontList[i];
		pFont->OnDeviceReset();
	}
}

HRESULT OyFontManager::FlushAllFonts()
{
	HRESULT hr = OY_OK;
	for( int i=0; i<m_OyFontList.Count(); i++ )
	{
		if( FAILED( m_OyFontList[i]->FlushAllVB() ) )
			hr = OY_FAIL;
	}
	return hr;
}

bool OyFontManager::AddFont(const char* strName, const char* strPath, int iSize, bool bBold, bool bItalic)
{
	if(iSize < 5)
		return false; // too small

	if(IsFontLoaded(strName))
		return true;

	Oy_TTF_Font* ptrNewFont = new Oy_TTF_Font( m_FTLib, m_pDevice, m_pOyD3D9 );
	if( !ptrNewFont->LoadTTFFontFromDisk(strName, strPath, iSize, bBold, bItalic) )
	{
		OyLog_Write_L1(OyXMLLog::Error, OyXMLLog::Engine, L"error: Can't Load TTFFont from disk!");
		delete ptrNewFont;
		ptrNewFont = NULL;
		return false;
	}

	if( !m_OyFontList.Push( ptrNewFont ) )
	{
		m_OyFontList.Resize( m_OyFontList.Size() + INIT_FONTLIST_ARRAY_NUM );
		m_OyFontList.Push( ptrNewFont );
	}	
		
	return true;
}

bool OyFontManager::IsFontLoaded(const char* strName)
{
	return GetFontByName(strName) != NULL;
}

Oy_TTF_Font* OyFontManager::GetFontByName(const char* strName)
{
	for( int i=0; i<m_OyFontList.Count(); i++ )
	{
		if( strcmp( strName, m_OyFontList[i]->GetFontName() ) == 0 )
			return m_OyFontList[i];
	}
	return NULL;
}

void OyFontManager::UnloadFont(const char* strName)
{
	for( int i=0; i<m_OyFontList.Count(); i++ )
	{
		if( strcmp( strName, m_OyFontList[i]->GetFontName() ) == 0 )
		{
			m_OyFontList[i]->UnloadFont();
			delete m_OyFontList[i];

			m_OyFontList[i] = m_OyFontList.Top();
			m_OyFontList.Pop();

			return;
		}
	}
}

int OyFontManager::GetActiveFontHeight()
{
	if(m_ActiveFont)
		return m_ActiveFont->GetFontHeight();

	return 0;
}

void OyFontManager::SetActiveFont( const char* strName )
{
	m_ActiveFont = GetFontByName(strName);
}

void OyFontManager::SetActiveFontHotSpot( float HotSpotx, float HotSpoty )
{
	if(m_ActiveFont)
	{
		m_ActiveFont->SetXHotspot(HotSpotx);
		m_ActiveFont->SetYHotspot(HotSpoty);
	}
}

void OyFontManager::SetActiveFontScale(float sx, float sy)
{
	if(m_ActiveFont)
	{
		m_ActiveFont->SetXScale(sx);
		m_ActiveFont->SetYScale(sy);
	}
}


// x,y Rendering text left-upper pos
bool OyFontManager::DoDrawTextInPos( const TCHAR* sText, float x, float y, DWORD FontColor,
	DWORD flag, float fFontSize, bool bKerning, bool bUnderl )
{
	bool ret = false;
	if(m_ActiveFont)
	{
		if( flag & OY_DRAWTEXT_MASK )
		{
			if( flag & OY_DRAWTEXT_SHADOW )
				ret = m_ActiveFont->DwawTextShadowInPos( sText, x, y, FontColor, flag, fFontSize, bKerning, bUnderl );
		}
		ret = m_ActiveFont->DrawTextInPos( sText, x, y, FontColor, flag, fFontSize, bKerning, bUnderl );
	}

	return ret;
}

void OyFontManager::PreCacheChar(const TCHAR* sText)
{
	if(m_ActiveFont)
		m_ActiveFont->PreCacheChar(sText);
}

